package com.xiyu.service.repository.setting.business;

import com.xiyu.service.model.setting.business.SettingBusinessFieldOption;
import com.xiyu.service.model.setting.business.SettingBusinessFieldOptionTable;
import org.babyfish.jimmer.spring.repository.JRepository;

import java.util.Optional;
import java.util.List;

public interface SettingBusinessFieldOptionRepository extends JRepository<SettingBusinessFieldOption, Long> {
    SettingBusinessFieldOptionTable settingBusinessFieldOptionTable = SettingBusinessFieldOptionTable.$;

    default List<SettingBusinessFieldOption> findByBusinessFieldId(Long businessFieldId){
        return sql().createQuery(settingBusinessFieldOptionTable)
                .where(settingBusinessFieldOptionTable.businessFieldId().eq(businessFieldId))
                .orderBy(settingBusinessFieldOptionTable.sort())
                .select(settingBusinessFieldOptionTable)
                .execute();
    }

    default void updateSort(Long id, Long sort){
        sql().createUpdate(settingBusinessFieldOptionTable)
                .where(settingBusinessFieldOptionTable.id().eq(id))
                .set(settingBusinessFieldOptionTable.sort(), sort)
                .execute();
    }

    Optional<SettingBusinessFieldOption> findByBusinessFieldIdAndName(Long businessFieldId, String name);

}